﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BaiDuSitemap.DataBase
{
    public partial class ucAdmin : UserControl
    {
        public ucAdmin()
        {
            InitializeComponent();
        }

        private void ucAdmin_SizeChanged(object sender, EventArgs e)
        {
            this.pPass.Location = new Point((this.Width - this.pPass.Width - btnLogin.Width) / 2, (this.Height - this.pPass.Height) / 3);
        }

        #region Event

        #region Button

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.tbUserID.Text.Length == 0)
            {
                MessageBox.Show("请输入账号");
                ShowMessage("账号不能为空");
                return;
            }

            if (DBO.DataBase.Login(this.tbUserID.Text.Trim(), QT.Tools.MD5(this.tbPass.Text.Trim())))
            {
                this.tbUserID.ReadOnly = true;
                this.tbPass.ReadOnly = true;
                this.btnLogin.Visible = false;
                ShowMessage("登入成功");
            }
            else
            {
                ShowMessage("登入失败");
                MessageBox.Show("登入失败，用户名或密码错误");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.btnLogin.Visible)
            {
                ShowMessage("您未登录到管理员状态");
                MessageBox.Show("请登录");
                return;
            }

            if (this.tbNewPass.Text != this.tbConfirmPass.Text)
            {
                ShowMessage("两次输入密码不一致");
                MessageBox.Show("两次输入密码不一致");
                return;
            }

            int result = DBO.DataBase.UpdatePass(this.tbUserID.Text, QT.Tools.MD5(this.tbNewPass.Text.Trim()));
            if ( result== 1)
            {
                MessageBox.Show("操作成功");
                ShowMessage("密码修改成功");
            }
            else
            {
                MessageBox.Show("操作异常");
                ShowMessage("密码修改异常，异常代码："+result);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion
        
        #endregion

        #region Method

        private void ShowMessage(string s)
        {
            this.lblMsg.Text = s;
            this.lblMsg.Location = new Point((this.pPass.Width - this.lblMsg.Width - this.btnLogin.Width) / 2, this.lblMsg.Location.Y);
        }

        #endregion
    }
}
